package com.quanquan.store.controller;

import DTO.PageDTO;
import com.quanquan.store.dto.ActivityDTO;
import com.quanquan.store.entity.Activity;
import com.quanquan.store.exception.ActivityException;
import com.quanquan.store.request.ActivityRequest;
import com.quanquan.store.service.ActivityService;
import enums.ResCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import utils.StringUtils;
import web.response.ResultBody;

import java.util.List;

/**
 * 功能描述:活动管理层
 *
 * @ClassName: ActivityController
 * @Author: xiaoqianh丶
 * @Date: 2019-03-26 17:33
 * @Version: V1.0
 */
@Controller
@RequestMapping("/activity")
public class ActivityController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private final ActivityService activityService;

    @Autowired
    public ActivityController(ActivityService activityService) {
        this.activityService = activityService;
    }

    /**
     * 添加活动信息
     *
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody addActivity(@RequestBody ActivityRequest requestBody) {

        String msg = null;
        msg = activityService.createActivity(requestBody);

        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), com.alibaba.fastjson.JSONObject.toJSON(msg));
    }


    /**
     * 查询活动列表（前端使用）
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody queryAllActivitys(@RequestBody ActivityRequest requestBody) {
        Integer page = requestBody.getPage() == null ? 0 : requestBody.getPage();
        Integer size = requestBody.getSize() == null ? 100 : requestBody.getSize();
        String recommend = requestBody.getRecommend();
        String activityStatus = requestBody.getActivityStatus();
        String placed = requestBody.getPlaced();
        List<Activity> activities = activityService.allActivity(page, size, activityStatus, recommend,placed);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), com.alibaba.fastjson.JSONObject.toJSON(activities));
    }

    @PostMapping(value = "/query_management")
    @ResponseBody
    public ResultBody queryAllActivitysForManagement(@RequestBody ActivityRequest requestBody) {

        PageDTO<Activity> activityPageDTO = null;
        try {
            activityPageDTO = activityService.allActivityForManagement(requestBody);
        } catch (ActivityException e) {
            e.printStackTrace();
        }

        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), activityPageDTO);

    }

    /**
     * 根据活动id查询活动详情
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public ResultBody queryActivityDetails(@RequestBody ActivityRequest requestBody) {
        String activityId = requestBody.getActivityId();
        String userId = requestBody.getUserId();
        ActivityDTO activityDTO = null;
        try {
            activityDTO = activityService.queryActivityDetails(activityId, userId);
        } catch (ActivityException e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg(), "查看活动详情失败");
        }

        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), com.alibaba.fastjson.JSONObject.toJSON(activityDTO));
    }


    /**
     * 根据商户id查找楼层
     *
     * @param request
     * @return
     */
    @PostMapping("/query_by_id")
    @ResponseBody
    public ResultBody queryById(@RequestBody ActivityRequest request) {
        if (StringUtils.isEmpty(request.getActivityId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "输入的活动id为空");
        }
        ActivityDTO activityDTO = activityService.query(request);
        if (activityDTO == null) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无此活动信息");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", activityDTO);
    }


    /**
     * 更新活动信息ResCodeEnum.SUCCESS.getCode()
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/update_activity")
    @ResponseBody
    public ResultBody updateActivity(@RequestBody ActivityRequest requestBody) {
        String activityId = requestBody.getActivityId();
        if (StringUtils.isEmpty(activityId)) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "输入的活动id为空");
        }
        String msg = null;
        try {
            msg = activityService.updateActivity(requestBody, activityId);
        } catch (ActivityException e) {
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg(), "更新活动失败");
        }

        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "成功", com.alibaba.fastjson.JSONObject.toJSON(msg));
    }


    /**
     * 删除活动
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/remove_activity")
    @ResponseBody
    public ResultBody removeActivity(@RequestBody ActivityRequest requestBody) {
        String activityId = requestBody.getActivityId();
        String msg = null;
        try {
            msg = activityService.removeActivity(activityId);
        } catch (ActivityException e) {
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg(), "删除活动失败");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), com.alibaba.fastjson.JSONObject.toJSON(msg));
    }

    @PostMapping("/hidden")
    public ResultBody hiddenActivity(@RequestBody ActivityRequest requestBody) {
        String activityId = requestBody.getActivityId();
        String msg = null;
        try {
            msg = activityService.setNotShow(activityId);
        } catch (ActivityException e) {
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg(), "设置隐藏失败");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), com.alibaba.fastjson.JSONObject.toJSON(msg));
    }


    @PostMapping("/check_activity_name")
    @ResponseBody
    public Boolean checkActivityName(@RequestBody ActivityRequest requestBody) {
        String activityName = requestBody.getActivityName();
        return activityService.findActivityByName(activityName);
    }


    /**
     * 后台用于绑定的活动列表
     *
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public ResultBody list() {
        List<ActivityDTO> activityDTOS = activityService.list();
        if (activityDTOS.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "NO_DATA");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", activityDTOS);
    }

}
